home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / net_src.arc / ftp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  4.5 KB  |  207 lines

  1. /* Stuff common to both the FTP server and client */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "timer.h"
  7. #include "tcp.h"
  8. #include "ftp.h"
  9. #include "telnet.h"
  10. #include "iface.h"
  11. #include "ax25.h"
  12. #include "lapb.h"
  13. #include "finger.h"
  14. #include "session.h"
  15. #ifdef    SYS5
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #ifdef    hp9000s500
  19. #include <sys/param.h>
  20. #endif
  21. #include <sys/inode.h>
  22. #endif
  23. #include "nr4.h"
  24.  
  25. #if    (ATARI_ST && MWC)
  26. #define    fclose    vclose        /* Take care of temp files -- hyc */
  27. #endif
  28.  
  29. /* FTP Data channel Receive upcall handler */
  30. void
  31. ftpdr(tcb,cnt)
  32. struct tcb *tcb;
  33. int16 cnt;
  34. {
  35.     register struct ftp *ftp;
  36.     struct mbuf *bp;
  37.     char c;
  38.  
  39.     ftp = (struct ftp *)tcb->user;
  40.     if(ftp->state != RECEIVING_STATE){
  41.         close_tcp(tcb);
  42.         return;
  43.     }
  44.     /* This will likely also generate an ACK with window rotation */
  45.     recv_tcp(tcb,&bp,cnt);
  46.  
  47. #if (UNIX || MAC || AMIGA || ATARI_ST)
  48.     if(ftp->type == ASCII_TYPE){
  49.         while(pullup(&bp,&c,1) == 1){
  50.             if(c != '\r')
  51.                 putc(c,ftp->fp);
  52.         }
  53.         return;
  54.     }
  55. #endif
  56.     while(bp != NULLBUF){
  57.         if(bp->cnt != 0)
  58.             fwrite(bp->data,1,(unsigned)bp->cnt,ftp->fp);
  59.         bp = free_mbuf(bp);
  60.     }
  61.  
  62.      if(ftp->fp != stdout && ferror(ftp->fp)){ /* write error (dsk full?) */
  63.          fclose(ftp->fp);
  64.          ftp->fp = NULLFILE;
  65.          close_self(tcb,RESET);
  66.      }
  67. }
  68. /* FTP Data channel Transmit upcall handler */
  69. void
  70. ftpdt(tcb,cnt)
  71. struct tcb *tcb;
  72. int16 cnt;
  73. {
  74.     struct ftp *ftp;
  75.     struct mbuf *bp;
  76.     register char *cp;
  77.     register int c;
  78.     int eof_flag;
  79. #ifdef    SYS5
  80.     struct stat ss_buf;
  81. #endif
  82.  
  83.     ftp = (struct ftp *)tcb->user;
  84.     if(ftp->state != SENDING_STATE){
  85.         close_tcp(tcb);
  86.         return;
  87.     }
  88.     if((bp = alloc_mbuf(cnt)) == NULLBUF){
  89.         /* Hard to know what to do here */
  90.         return;
  91.     }
  92.     eof_flag = 0;
  93.     if(ftp->type == IMAGE_TYPE){
  94.         bp->cnt = fread(bp->data,1,cnt,ftp->fp);
  95.         if(bp->cnt != cnt)
  96.             eof_flag = 1;
  97.     } else {
  98.         cp = bp->data;
  99.         while(cnt > 1){
  100.             if((c = getc(ftp->fp)) == EOF){
  101.                 eof_flag=1;
  102.                 break;
  103.             }
  104. #if (defined(CPM) || defined(MSDOS))
  105.             /* ^Z is CP/M's text EOF marker, and it is sometimes used
  106.              * by MS-DOS editors too
  107.              */
  108.             if(c == CTLZ){
  109.                 eof_flag = 1;
  110.                 break;
  111.             }
  112. #endif
  113. #if (defined(UNIX) || defined(MAC) || defined(AMIGA) || defined(ATARI_ST))
  114.             if(c == '\n'){
  115.                 *cp++ = '\r';
  116.                 bp->cnt++;
  117.                 cnt--;
  118.             }
  119. #endif
  120.             *cp++ = c;
  121.             bp->cnt++;
  122.             cnt--;
  123.         }
  124.     }
  125.     if(bp->cnt != 0)
  126.         send_tcp(tcb,bp);
  127.     else
  128.         free_p(bp);
  129.  
  130.     if(eof_flag){    /* EOF seen */
  131. #ifdef    UNIX
  132. #ifdef    SYS5
  133. #ifndef    hp9000s500
  134. /* If ftp->fp points to an open pipe (from dir()) it must be closed with */
  135. /* pclose().  System V fstat() can tell us if this was a pipe or not. */
  136.         if (fstat(fileno(ftp->fp), &ss_buf) < 0)
  137.             perror("ftpdt: fstat");
  138.         if ((ss_buf.st_mode & IFIFO) == IFIFO)
  139.             pclose(ftp->fp);    /* close pipe from dir */
  140.         else
  141.             fclose(ftp->fp);
  142. #else    /* hp9000s500 */
  143. /* HP-UX 5.21 on the 500 doesn't understand IFIFO, since we probably don't *.
  144. /* care anyway, treat it like BSD is treated... */
  145.         if (pclose(ftp->fp) < 0)
  146.             fclose(ftp->fp);
  147. #endif    /* hp9000s500 */
  148. #else    /* SYS5 */
  149. /* Berkeley Unix can't tell if this was a pipe or not.  Try a pclose() */
  150. /* first.  If this fails, it must have been an open file. */
  151.         if (pclose(ftp->fp) < 0)
  152.             fclose(ftp->fp);
  153. #endif    /* SYS5 */
  154. #else    /* UNIX */
  155. /* Anything other than Unix */
  156.         fclose(ftp->fp);
  157. #endif    /* UNIX */
  158.         ftp->fp = NULLFILE;
  159.         close_tcp(tcb);
  160.     }
  161. }
  162. /* Allocate an FTP control block */
  163. struct ftp *
  164. ftp_create(bufsize)
  165. unsigned bufsize;
  166. {
  167.     void ftp_delete();
  168.     register struct ftp *ftp;
  169.  
  170.     if((ftp = (struct ftp *)calloc(1,sizeof (struct ftp))) == NULLFTP)
  171.         return NULLFTP;
  172.     if(bufsize != 0 && (ftp->buf = malloc(bufsize)) == NULLCHAR){
  173.         printf("called by ftp_create\n");fflush(stdout);
  174.         ftp_delete(ftp);
  175.         printf("called by ftp_create\n");fflush(stdout);
  176.         return NULLFTP;
  177.     }
  178.     ftp->state = COMMAND_STATE;
  179.     ftp->type = ASCII_TYPE;    /* Default transfer type */
  180.     return ftp;
  181. }
  182. /* Free resources, delete control block */
  183. void
  184. ftp_delete(ftp)
  185. register struct ftp *ftp;
  186. {
  187.         int i;
  188.  
  189.     if(ftp->fp != NULLFILE && ftp->fp != stdout)
  190.         fclose(ftp->fp);
  191.     if(ftp->data != NULLTCB)
  192.         del_tcp(ftp->data);
  193.     if(ftp->username != NULLCHAR)
  194.         free(ftp->username);
  195.         for (i = 0; i < MAXPATH; i++)
  196.       if(ftp->path[i] != NULLCHAR)
  197.         free(ftp->path[i]);
  198.     if(ftp->buf != NULLCHAR)
  199.         free(ftp->buf);
  200.     if(ftp->cd != NULLCHAR)
  201.         free(ftp->cd);
  202.     if(ftp->session != NULLSESSION)
  203.         freesession(ftp->session);
  204.     free((char *)ftp);
  205. }
  206.  
  207.